https://leetcode.com/problems/remove-duplicates-from-sorted-array/
一個排列好的陣列,回傳差異整數的長度。
先判斷陣列長度,利用for迴圈探索中用一個變數紀錄目前長度並當成替代的索引值,但因從索引一開始探索因此要在回傳時將變數紀錄加一。
int removeDuplicates(int* nums, int numsSize) {
if (numsSize == 0)
return 0;
int i=0,j;
for(j=1; j<numsSize; j++ )
{
if(nums[j] != nums[i])
{
i++;
nums[i] = nums[j];
}
}
return i+1;
}
var removeDuplicates = function(nums) {
if(nums == null || nums.length == 0) return 0;
if(nums.length == 1) return 1;
var count = 0;
for(var i = 1 ; i < nums.length ; i++){
if(nums[count] != nums[i]){
count++;
nums[count] = nums[i];
}
}
return ++count;
};
https://github.com/SIAOYUCHEN/leetcode
https://ithelp.ithome.com.tw/users/20100009/ironman/2500
https://ithelp.ithome.com.tw/users/20113393/ironman/2169
https://ithelp.ithome.com.tw/users/20107480/ironman/2435
https://ithelp.ithome.com.tw/users/20107195/ironman/2382
https://ithelp.ithome.com.tw/users/20119871/ironman/2210
https://ithelp.ithome.com.tw/users/20106426/ironman/2136
Life is a multiple-choice question; often what confuses you is not the question, but the options available.
人生就像一道選擇題,往往讓你困惑的不是題目,而是選項